package com.ymatou.iapi.sellerproduct.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.AutoRefreshProductsMessageHandlerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.YMTDateUtil;

//class TaskWithResult implements Callable<String> { 
//	private int sellerId;
//	private String sellerName;
//    public TaskWithResult(int _sellerId,String _sellerName) {  
//        this.sellerId=_sellerId;  
//        this.sellerName = _sellerName;
//    }  
//      
//    @Override  
//    public String call() throws Exception {  
//    	Ts_AutoRefreshProductsMessageHandler handler = new Ts_AutoRefreshProductsMessageHandler();
//    	return handler.addNewProducthistory(sellerId, sellerName);
//    }  
//}  

@TestSuite
public class Ts_AutoRefreshProductsMessageHandler{
	private static AutoRefreshProductsMessageHandlerCall autoRefreshProductsMessageHandlerCall;
	//private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static Integer sellerId = 3383;
	
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家登录商家后台,重新上架商家所有符合条件商品");
	}

	@Before
	public void caseUp() {
		autoRefreshProductsMessageHandlerCall=new AutoRefreshProductsMessageHandlerCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(autoRefreshProductsMessageHandlerCall.getOpurl());
	}

	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategory_001() {
		Logger.start(true,"商家登录商家后台,重新上架商家所有符合条件商品");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			String startTimeforsql = YMTDateUtil.getBeforeOrNextHour(0, "yyyy-MM-dd HH");	//sql没有时差
			String endTimeforsql = YMTDateUtil.getBeforeOrNextHour(168, "yyyy-MM-dd HH");
			
			//发布一个6天前立即上架商品
			String productID1 = addNewProduct10dayago(sellerId, sellerName);
//			String productID2 = addNewProducthistory(sellerId, sellerName);
//			for (int i = 0; i < 10; i++) {
//				 int num =  i; 
//				 addNewProducthistory(sellerId, sellerName);
//			}

			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerBean, maplist);			
			autoRefreshProductsMessageHandlerCall.setData(autoRefreshProductsMessageHandlerBean);
			autoRefreshProductsMessageHandlerCall.callService();
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerCall.getReturnData();
			Logger.verifyEquals("\"ok\"", result, "返回报文："+ result);
			
			YMTDateUtil.waitTime(10);
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			String svalidStartbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validStart"));
			String svalidEndbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validEnd"));	
			//断言
			Logger.verifyEquals(startTimeforsql, svalidStartbysql1, "SQL对比商品开始销售时间");
			Logger.verifyEquals(endTimeforsql, svalidEndbysql1, "SQL对比商品结束销售时间");
			
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productBasicInfo.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productBasicInfo.get("ValidEnd"), 10000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 10000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 10000);				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategory_002() {
		Logger.start(false,"5天前的商品不会自动上架");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			
			//发布一个6天前立即上架商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID1 =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
		
			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerBean, maplist);			
			autoRefreshProductsMessageHandlerCall.setData(autoRefreshProductsMessageHandlerBean);
			autoRefreshProductsMessageHandlerCall.callService();
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerCall.getReturnData();
			Logger.verifyEquals("\"ok\"", result, "返回报文："+ result);
			
			YMTDateUtil.waitTime(10);
			
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			//断言
			AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productIDsqllist1.get(0).get("validStart"), 1000);
			AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productIDsqllist1.get(0).get("validEnd"), 1000);
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productBasicInfo.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productBasicInfo.get("ValidEnd"), 1000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productDetailModel.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productDetailModel.get("ValidEnd"), 1000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productListingModel.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productListingModel.get("ValidEnd"), 1000);				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategory_003() {
		Logger.start(true,"过期商品自动上架");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			String startTimeforsql = YMTDateUtil.getBeforeOrNextHour(0, "yyyy-MM-dd HH");	//sql没有时差
			String endTimeforsql = YMTDateUtil.getBeforeOrNextHour(168, "yyyy-MM-dd HH");
			
			//发布一个10天前立即上架商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct10dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID1 =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
			
			
			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerBean, maplist);			
			autoRefreshProductsMessageHandlerCall.setData(autoRefreshProductsMessageHandlerBean);
			autoRefreshProductsMessageHandlerCall.callService();
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerCall.getReturnData();
			Logger.verifyEquals("\"ok\"", result, "返回报文："+ result);
			
			YMTDateUtil.waitTime(10);
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			String svalidStartbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validStart"));			
			String svalidEndbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validEnd"));	
			//断言
			Logger.verifyEquals(startTimeforsql, svalidStartbysql1, "SQL对比商品开始销售时间");
			Logger.verifyEquals(endTimeforsql, svalidEndbysql1, "SQL对比商品结束销售时间");
			
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productBasicInfo.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productBasicInfo.get("ValidEnd"), 10000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 10000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 10000);				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_AmendProductBrandAndCategory_004() {
//		Logger.start(true,"商家登录商家后台,重新上架商家所有符合条件商品");
//		try {
//			//TODO 补全case代码
//			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
//			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
//			String startTimeforsql = YMTDateUtil.getBeforeOrNextHour(0, "yyyy-MM-dd HH");	//sql没有时差
//			String endTimeforsql = YMTDateUtil.getBeforeOrNextHour(168, "yyyy-MM-dd HH");
//			
//			//发布一个6天前立即上架商品
////			String productID1 = addNewProducthistory(sellerId, sellerName);
////			String productID2 = addNewProducthistory(sellerId, sellerName);
////			for (int i = 0; i < 10; i++) {
////				 int num =  i; 
////				 addNewProducthistory(sellerId, sellerName);
////			}
//			List<Future> list = new ArrayList<Future>(); 
//			List<String> productIdList = new ArrayList<String>();
//			 // 创建一个线程池  
//			   ExecutorService pool = Executors.newFixedThreadPool(100);  
//			for(int i = 0; i < 16000; i++){
//				Callable c = new TaskWithResult(sellerId,sellerName);  
//				Future future = pool.submit(c);
//				list.add(future);
//			}
////			System.console().printf("List<Future>的个数为"+ list.size());
//			  pool.shutdown();
////			for(Future f:list){
////				productIdList.add(f.get().toString());
////			}
////			String productID1 = list.get(0).get().toString();
//			//更新登陆记录为过期
//			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
//			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
//			
//			//Call 登陆自动上架
//			Map<String,Object> maplist = new HashMap<String, Object>();		    
//			maplist.put("UserID",sellerId);	
//			JSONObject autoRefreshProductsMessageHandlerBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
//			autoRefreshProductsMessageHandlerBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerBean, maplist);			
//			autoRefreshProductsMessageHandlerCall.setData(autoRefreshProductsMessageHandlerBean);
//			autoRefreshProductsMessageHandlerCall.callService();
//			//返回报文断言
//			String result = autoRefreshProductsMessageHandlerCall.getReturnData();
//			Logger.verifyEquals("\"ok\"", result, "返回报文："+ result);
//		
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}	
	
	
	
	

	
	
	
		
	/**
	 * 发布一个立即上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个未上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductNotOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个10天前自动上架的商品
	 * @return 商品id
	 * @throws Exception
	 */
	public static String addNewProduct10dayago(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct10dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	

}